package com.example.streamdemo.stream;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toList;

public class MyStreamTest {
    public static void main(String[] args) {
        List<Integer> list = Arrays.asList(1, 5, 2, 8, 4, 0, 2, 3, 1, 6, 3, 7, 1);
        list.stream().filter(e -> e % 2 == 0).map(e -> e + 1).forEach(System.out::print);
        System.out.println();
        list.parallelStream().forEach(System.out::print);
        List<Integer> collect = list.parallelStream().collect(toList());
        System.out.println(collect.toString());
    }

    public static class Main1 {
        public static void main(String[] args) {
            List<String> strings = Arrays.asList("1", "23", "23423", "1231", "345678978", "1111", "");
            strings.stream().map(String::length).forEach(e -> System.out.printf("%-3d", e));
            strings.stream().map(e -> e.split("")).distinct().forEach(e -> {
                for (String s : e) {
                    System.out.print(s);
                }
                System.out.println();
            });

            strings.stream().map(e -> e.split("")).flatMap(Arrays::stream).
                    distinct().collect(toList()).forEach(System.out::println);
            strings.stream().map(e -> e.split("")).map(Arrays::stream).
                    distinct().collect(toList()).forEach(System.out::println);
        }
    }

    public static class Main2 {
        public static void main(String[] args) {
            int initialCapacity = 1000000;
            List<Integer> list = new ArrayList<>(initialCapacity);
            for (int i = 0; i < initialCapacity; i++) {
                list.add(i + 1);
            }
            long start = System.currentTimeMillis();
            test1(list);
//            test2(list);
            long end = System.currentTimeMillis();
            System.out.println("timeCost " + (end - start));
        }

        public static <T> void test1(List<T> list) {
            list.forEach(System.out::println);
        }

        public static <T> void test2(List<T> list) {
            for (T i :
                    list) {
                System.out.println(i);
            }
        }
    }
}
